<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/font-awesome/5.8.2/css/all.min.css" rel="stylesheet">
</head>

<body>
    <div class="jumbotron jumbotron-fluid">
        <div class="container text-center">
            <h1 class="display-4">翻译</h1>
            <p class="lead">中英互译</p>
        </div>
    </div>
    <div class="container">
        <div class="row justify-content-between mb-3">
            <div class="col-3">
                <div class="btn-group btn-block" role="group" aria-label="Basic example" id="btn_if">
                    <button type="button" class="btn btn-primary" disabled>中文</button>
                    <button type="button" class="btn btn-light" disabled>英文</button>
                </div>
            </div>
            <div class="col-3">
                <button type="button" class="btn btn-block btn-secondary" onclick="switchLan()" id="fx">
                    <i class="fas fa-arrows-alt-h"></i>
                </button>
            </div>
            <div class="col-3">
                <div class="btn-group btn-block" role="group" aria-label="Basic example" id="btn_that">
                    <button type="button" class="btn btn-primary" disabled>英文</button>
                    <button type="button" class="btn btn-light" disabled>中文</button>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-6">
                <div class="form-group">
                    <textarea class="form-control" id="zw" rows="5"></textarea>
                </div>
            </div>
            <div class="col-6">
                <div class="form-group">
                    <textarea class="form-control" id="yw" rows="5" readonly="readonly"></textarea>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-3">
                <button type="button" class="btn btn-primary btn-block btn-lg" onclick="zhToEn()">翻译</button>
            </div>
        </div>
    </div>




    <script src="js.js"></script>
    <!-- 备份 -->
    <!-- <div class="col-1">
        <h1>翻译</h1>
        <p>中英互译</p>
    </div>
    <div class="col-2">
        <span class="col-3">中文</span>
        <span class="ys">英语</span>
        <button id="fx" onclick="switchLan()">⇆</button>
        <span class="col-3">英语</span>
        <span class="ys">中文</span>

        <div class="col-5">
            <textarea id="zw" cols="70" rows="15"></textarea>
            <textarea id="yw" cols="70" rows="15"></textarea>
        </div>
        
        <div>
            <button class="bt" onclick="zhToEn()">翻译</button>
        </div>
    </div>

    <script>
        //中文
        var data_zh = ['你知道吗', '六月', '一日', '星期一', '是', '儿童节'];
        //英语
        var data_en = ['do you know', 'June', 'One day', 'Monday', 'Yes', 'Children\'s day'];
        // 切换按钮
        var btn = document.querySelectorAll("div span");
        //中英文互译的函数
        function zhToEn() {
            var if_text=false;
            var s = document.querySelector("#zw").value;
            var j = document.querySelectorAll("#yw");
            for (var i = 0; i <= data_zh.length; i++) {
                if (s === data_zh[i]) {
                    if (data_zh[i] === s) {
                        if_text=true;
                        document.querySelector("#yw").value = data_en[i];
                        if(lang='en'){
                            switchLan();
                        }
                    }
                } else {
                    if (data_en[i] === s) {
                        if_text=true;
                        document.querySelector("#yw").value = data_zh[i];
                        if(lang='zh'){
                            switchLan();
                        }
                    }
                }
            }
            if(!if_text){
                j.value="";
                alert('无法翻译');
            }
        }

        // 按钮转换
        var lang = 'zh';
        function switchLan() {

            if (lang == 'zh') {
                lang = 'en';
            } else {
                lang = 'zh';
            }
            for (var i = 0; i < 2; i++) {
                btn[0].classList.remove('col-3');
                btn[1].classList.remove('col-3');
                btn[2].classList.remove('col-3');
                btn[3].classList.remove('col-3');
            }
            switch (lang) {
                case 'zh':
                    btn[0].classList.add('col-3');
                    btn[1].classList.remove('col-3');
                    btn[2].classList.add('col-3');
                    btn[3].classList.remove('col-3');
                    break;
                case 'en':
                    btn[0].classList.remove('col-3');
                    btn[1].classList.add('col-3');
                    btn[2].classList.remove('col-3');
                    btn[3].classList.add('col-3');
                    break;
            }
        }
    </script> -->

</body>

</html>